/*
 * @(#)MailSender.java May 17, 2007
 * 
 * Copyright 2007 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */
package com.cnc.fd.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.cnc.fd.Constants;
import com.cnc.bas.util.Log;


/**
 * @author 阿闲
 * @date   May 17, 2007
 * @version 1.0
 */
public class MailSender extends Thread {
	
	Log log = Log.getLogger(MailSender.class);
	
	private String content;
	private String subject;
	private String[] toAddress;
	
	public MailSender(String subject, String content, String[] toAddress){
		this.subject = subject;
		this.content = content;
		this.toAddress = toAddress;
	}

	public void run(){
		
		Transport transport = null;
		
		try{			
			
		Properties props = System.getProperties();
		
		props.put("mail.smtp.host", FdProperties.getProperty("SMTP_HOST"));
		
		props.put("mail.smtp.localhost", FdProperties.getProperty("SMTP_HOST"));		
		
		props.put("mail.smtp.auth", "true");
		
		Session session = Session.getDefaultInstance(props);
		
		MimeMessage mimeMsg = new MimeMessage(session);
		
		mimeMsg.setSubject(subject);
		
		mimeMsg.setContent(content, "text/html;charset=GBK");

		mimeMsg.setFrom(new InternetAddress(Constants.SENDER_FROM));	
	
			
		
		transport = session.getTransport("smtp");		
		
		transport.connect(FdProperties.getProperty("SMTP_HOST"), FdProperties.getProperty("SENDER_USER"), FdProperties.getProperty("SENDER_PWD"));		
		
		int sendSuccess = 0;
		for(int i=0; i<toAddress.length; i++){
            mimeMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress[i]));
            try{
                transport.sendMessage(mimeMsg, mimeMsg
                        .getRecipients(Message.RecipientType.TO));
                sendSuccess ++;
            }
            catch(Exception ex){
                log.logError("send mail to " + toAddress[i] + " fail!", ex);
            }
        }
		
		log.logDebug("mailsender send success [count: " + sendSuccess + "], failure [count:" + (toAddress.length - sendSuccess) + "], mail subject:" + subject);
		
		}catch(Exception e){			
			log.logError("mailsender send fail: " + subject, e);			
		}finally{
			
			try{
				if(transport != null)
					transport.close();
			}catch(Exception e){
				//do nothing
			}
		}
	}
	
	public static void main(String[] args){
		String toAddress = Constants.OPERATOR_MAIL_ADDRESS;
		String customerAddress = MailProperty.getAddress("test");
		if(customerAddress != null){
			toAddress += "," + customerAddress;
		}
		MailSender sender = new MailSender("邮件测试","测试邮件",toAddress.split(","));
		sender.start();
	}
}


